/*获取地址url参数*/
window.getUrlParam = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return r[2]; return null; //返回参数值
}

/**调用接口*/
//window.CONST_SERVER_DOMAIN = 'http://192.168.0.123:8080/mobile'; //本地环境
//window.CONST_SERVER_DOMAIN = 'http://192.168.0.123:8080/mobile'; //测试环境
//window.CONST_SERVER_DOMAIN = 'http://192.168.0.123:8080/mobile'; //生产环境

window.aTimeOut = null;
//需要发送session的请求方法;
window.httpAjaxSession = function(ajaxType, aPostPath, aRequestData, aSuccessFunc, aErrorFunc, aTimeOut){
	
	$.ajax({
			type: ajaxType,
			url: "../../../" + aPostPath +"?sessionId=" + getcache("sessionId"),
			 //"?sessionId=" + getcache("sessionId"), "?sessionId=c875ef37aed44d1b85565b0694013cad"
			dataType:"json",
			data:aRequestData,
			timeout:aTimeOut == null?30*1000:aTimeOut,
			beforeSend:function(request){
				//request.setRequestHeader("sessionId", 'fc1f443890c74681b053ca9897a47874');
			   request.setRequestHeader("Content-Type",'application/json;charset=UTF-8');
			   $("body").append("<div class='atimeout'><div class='loader'><div class='loader-inner ball-pulse'><div></div><div></div><div></div></div></div><br/><br/><br/>加载中...</div>")
			},
			success:aSuccessFunc,
			error:function(){
                   //aTimeOutError();
                   console.log('网络错误');
                },
			complete:function(){
				   $(".atimeout").remove();
				},
			});
}

/*超时提示*/
window.aTimeOutError = function(){
	  $(".atimeout").remove();
	  $("body").append("<div class='zhezhao'><div class='contact_zhezhao'><p><img src='../images/refresh.png' onClick='timeFun()'></p><span>网络出现错误,点击刷新</span><div class='clear'></div></div>");
}

//读取缓存
window.getcache = function(key) {
    if (sessionStorage[key]){
		var result, val = sessionStorage.getItem(key);
		if (val){
			result = val;
		}
		return result;
	}
    else
        return "";
}
//读取缓存
window.removeCache = function(key) {
    sessionStorage.removeItem(key);
}
//更改缓存
window.setcache = function(key, v) {
    sessionStorage.setItem(key, v);
}
//删除缓存
window.removecache = function(key) {
    sessionStorage.removeItem(key);
}

//数字格式化方法;
window.FormatMoney = function(s, n) {
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
	var t = "";
	for (i = 0; i < l.length; i++) {
		t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	}
	return t.split("").reverse().join("") + "." + r;
}